﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace HolterDatabase
{
    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is System.DBNull)
            {
                return "";
            }
            var dateTime = (System.DateTime)value;
            return dateTime.Day + "/" + dateTime.Month + "/" + dateTime.Year;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var valueString = (string)value;
            string[] tokens;
            if (valueString.Contains('.')) 
            {
                tokens = valueString.Split('.');
            } else 
            {
                tokens = valueString.Split('/');
            }

            if (tokens.Length != 3)
            {
                return null;
            }

            return new DateTime(int.Parse(tokens[2]), int.Parse(tokens[1]), int.Parse(tokens[0]));
        }
    }
}
